﻿(function ($) {
    String.prototype.ToDateTime = function () {
        var dateStr = this.replace("T", " ").replace(/\//g, "-");
        var timeArr = dateStr.split(" ");
        var d = timeArr[0].split("-");
        var t = timeArr[1].split(":");
        return new Date(d[0], d[1] - 1, d[2], t[0], t[1], t[2]);
    }

    Date.prototype.Format = function (fmt) { //author: meizz 
        var o = {
            "m+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "i+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    Date.prototype.GetIntervalText = function() {
        var dateNew = new Date();
        var dateOld = this;
        var interval = dateNew.getTime() - dateOld.getTime();
      var result = "";

      if (interval < 1000 * 60) {
        //小于一分钟
          result = Math.floor(interval / (1000))+"秒";
      } else if (interval < 1000 * 60 * 60) {
        //小于一小时
          result = Math.floor(interval / (1000 * 60))+"分";
      } else if (interval < 1000 * 60 * 60 * 24) {
        //小于一天
        result = Math.floor(interval / (1000 * 60 * 60)) + "小时";
      } else  {
        //小于一天
        result = Math.floor(interval / (1000 * 60 * 60 * 24)) + "天";
      }

      return result;
    }

  Number.prototype.ToFixationStr = function(lth) {
        var temp = "0000000000" + this;
        temp = temp.substring(temp.length - lth);//输出001就-3

        return temp;
    }


    //Notification handler
    abp.event.on('abp.notifications.received', function (userNotification) {
        abp.notifications.showUiNotifyForUserNotification(userNotification);

        //Desktop notification
        Push.create("HonkSoft", {
            body: userNotification.notification.data.message,
            icon: abp.appPath + 'images/app-logo-small.png',
            timeout: 6000,
            onClick: function () {
                window.focus();
                this.close();
            }
        });
    });

    //serializeFormToObject plugin for jQuery
    $.fn.serializeFormToObject = function () {
        //serialize to array
        var data = $(this).serializeArray();

        //add also disabled items
        $(':disabled[name]', this).each(function () {
            data.push({ name: this.name, value: $(this).val() });
        });

      $(":checkbox", this).each(function () {
          var name = this.name;
        var value = this.checked;

        var obj = data.filter(function (d) {
          return d.name === name && d.value !== value;
        });

        if (obj != null) {
          $(obj).each(function () {
            this.value = value;
          });
        }

        //   data[name] = value;
      });

        //map to object
        var obj = {};
        data.map(function (x) { obj[x.name] = x.value; });

        return obj;
    };

    //Configure blockUI
    if ($.blockUI) {
        $.blockUI.defaults.baseZ = 2000;
    }


})(jQuery);